package test.org.offsync.nio

import java.nio.channels.{SelectionKey => JavaSelectionKey}
import org.junit.runner.RunWith
import org.offsync.nio.Selector

@RunWith(classOf[org.specs2.runner.JUnitRunner])
class SpecSelector extends org.specs2.mutable.SpecificationWithJUnit {
  "Selector" should {
    val selector = new Selector(JavaSelectionKey.OP_ACCEPT)
    "allow execution in selector thread" in {
      var counter = 0
      var action1 = 0
      var action2 = 0
      val thread = new Thread() {
        override def run() {
          selector.run()
        }
      }
      selector.execute {
        counter += 1
        action1 = counter
      }
      thread.start()
      counter += 1
      action2 = counter
      Thread.sleep(100)
      selector.stop()
      thread.join()
      action2 must_== 1
      action1 must_== 2
    }
  }
}
